package cn.caidabao.camunda_demo.util;

import cn.caidabao.camunda_demo.pojo.po.TCaiProcess;
import cn.caidabao.camunda_demo.pojo.po.TCaiProcessFormField;
import cn.caidabao.camunda_demo.service.ITCaiProcessFormFieldService;
import cn.caidabao.camunda_demo.util.show.ContentFieldConverter;
import cn.caidabao.camunda_demo.util.show.DefaultConverter;
import cn.caidabao.camunda_demo.util.show.UserConverter;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.AllArgsConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author cjn
 * @since 2024-07-16 9:27
 */
@Component
@AllArgsConstructor
public class ContentShowUtil {

    private final ITCaiProcessFormFieldService caiProcessFormFieldService;
    private final ApplicationContext applicationContext;


    public List<JSONObject> getForm(List<TCaiProcess> processes){
        if (CollectionUtils.isEmpty(processes)){
            return Collections.EMPTY_LIST;
        }
        Integer caiFormType = processes.get(0).getCaiFormType();
        List<TCaiProcessFormField> caiProcessFormFields = null;
        if (caiFormType.equals(1)){
            caiProcessFormFields = caiProcessFormFieldService.getFieldsByClass(processes.get(0).getCaiFormTag());
        }else {
            caiProcessFormFields = caiProcessFormFieldService.getFieldsByFormId(processes.get(0).getCaiFormTag());
        }
        Map<String, TCaiProcessFormField> fieldMap = caiProcessFormFields.stream().collect(Collectors.toMap(TCaiProcessFormField::getFieldName, field -> field));
        List<JSONObject> res = new ArrayList<>();
        for (TCaiProcess process : processes) {
            String content = process.getContent();
            JSONObject jsonObject = JSON.parseObject(content);
            JSONObject form = new JSONObject();
            jsonObject.forEach((a,b)->{
                TCaiProcessFormField formField = fieldMap.get(a);
                handleForm(a,b,form,formField);
            });
            form.put("createTime",process.getCreateTime());
            form.put("ownerId",process.getOwnerId());
            form.put("ownerType",process.getOwnerType());
            form.put("processId",process.getProcessId().toString());
            form.put("processType",process.getProcessType());
            res.add(form);
        }
        return res;

    }

    private void handleForm(String key, Object value, JSONObject form, TCaiProcessFormField formField) {
        String detailFieldName = formField.getDetailFieldName();
        if (StringUtils.isNotBlank(detailFieldName)){
            ContentFieldConverter converter = getConvertor(formField.getShowType());
            Map<String, Object> convert = converter.convert(Arrays.asList(value));
            form.put(detailFieldName,convert.get(value.toString()));
        }
        form.put(key,value);
    }

    private ContentFieldConverter getConvertor(String showType) {
        if (showType.equals("USER_NAME")){
            return applicationContext.getBean(UserConverter.class);
        }else if (showType.equals("DEFAULT")){
            return applicationContext.getBean(DefaultConverter.class);
        }
        return null;
    }


}
